import HttpUtils from '../HttpUtils/HttpUtils';// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		code: 0,
		folderCode: 0,
		pageIndex: 1,
		pageItems: 10,
		totalPages: 1,
		topicCode: 0,
		content: {},
		subContentList: []

	},
	hotTap(e) {     //新闻  议题
		var topicCode = e.currentTarget.dataset.topiccode;
		this.setData({
			pageIndex: 1,
			subContentList: []
		});
		this.loadSubContentList(topicCode);
	},
	topicDetail(e) {
		var folderCode = this.data.folderCode;
		var type = e.currentTarget.dataset.type;
		var code = this.data.code;
		var subCode = e.currentTarget.dataset.subcode;
		var name = e.currentTarget.dataset.name;
		var detailUrl = this.getUrl("topicDetail", subCode, folderCode, name, code);
		if (detailUrl.length > 0) {
			wx.navigateTo({
				url: detailUrl + "&topicName=" + this.data.name
			});
		} else {
			this.showPageMsg("tips", "该内容类型详情页暂未开发");
		}
	},
	onLoad(params) {
		var folderCode = params.folderCode;
		var code = params.code;
		var name = params.topicName;
		this.setData({
			"code": code,
			"folderCode": folderCode,
			"name": name
		});
		//获取内容详情
		this.loadDetail();
	},
	loadDetail: function(e) {
		//调用内容详情接口,获取内容详情
		var newUrl = getApp().globalData.dangjianUrl + "/content/contentQuery";
		var params = {};
		params["code"] = this.data.code + "";
		params["contentName"] = this.data.name + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["subCode"] = "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "内容详情加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var content = respData.data.content;
					var thumbnail = content.thumbnail;
					if (null == thumbnail || "" == thumbnail) {
						var imageFiles = content.imageFiles;
						for (var j in imageFiles) {
							thumbnail = imageFiles[j].url;
							break;
						}
					}
					this.setData({
						"content.url": thumbnail,
						"content.topic.topicItems": content.topic.topicItems

					});
					//加载栏目下内容列表
					var topicCode = content.topic.topicItems[0].topicCode;
					this.loadSubContentList(topicCode);
        } else {
          this.showPageMsg("alert", respData.data.result.description) 
				}
			}
		);
	},
	loadSubContentList: function(e) {
		//调用专题详情接口,获取专题下的内容列表
		var topicCode = 0;
		var topicName = "";
		if ("string" == typeof (e)) {
			topicCode = e;
		} else {
			//表示从页面上点击过来的,是一个对象
			topicCode = e.currentTarget.dataset.topicCode;
			topicName = e.currentTarget.dataset.topicname;
		}
		var newUrl = getApp().globalData.dangjianUrl + "/content/topicsContentQuery";
		var params = {};
		params["code"] = this.data.code + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["topicsCode"] = topicCode + "";
		params["topicsName"] = topicName + "";
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + ""; 
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "专题栏目内容列表加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var contentItems = respData.data.topic;
					console.log(contentItems);
					var totalPages = contentItems.totalPages;
					contentItems = contentItems.contentItems;
					var topicItems = this.data.content.topic.topicItems;
					for (var index in topicItems) {
						var items = topicItems[index];
						if (topicCode == items.topicCode) {
							items.className = "titOn";
						} else {
							items.className = "";
						}
					}
					for (var index in contentItems) {
						var items = contentItems[index];
						items.sourceFrom = null == items.sourceFrom ? "华数" : items.sourceFrom;
						var imageFiles = items.imageFiles;
						for (var j in imageFiles) {
							if (imageFiles[j].type == "240d" || imageFiles[j].type == "180d") {
								imageFiles[0] = imageFiles[j];
								break;
							}
						}
					}
					var subContentList = this.data.subContentList;
					subContentList = subContentList.concat(contentItems);
					this.setData({
						"topicCode": topicCode,
						"subContentList": subContentList,
						"content.topic.topicItems": topicItems,
						"totalPages": totalPages
					});
        } else {
          this.showPageMsg("alert", respData.data.result.description) 
				}
			}
		);
	},
	onReachBottom: function(e) {
		var pageIndex = this.data.pageIndex + 1;
		var totalPages = this.data.totalPages;
		if (totalPages >= pageIndex) {
			this.setData({
				"pageIndex": pageIndex
			});
			this.loadSubContentList(this.data.topicCode);
		} else {
			this.showPageMsg("tips", "已经是最后一页了");
		}
		wx.hideLoading();
	},
	onPullDownRefresh: function(e) {
		this.setData({
			pageIndex: 1,
			subContentList: []
		});
		this.loadSubContentList(this.data.topicCode);
		wx.stopPullDownRefresh();
	}
});
